package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TabOne2Activity
import client.comm.baoding.ui.TuiGuangActivity
import client.comm.baoding.widget.HorizontalPageLayoutManager
import client.comm.baoding.widget.PagingScrollHelper
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import client.comm.commlib.getScreenWidth
import client.comm.commlib.utils.StoreUtil

class TabTeamHeaderAdapter(val manager: RequestManager, val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    var tdBean1: TdBean? = null

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_tdheader,
                parent,
                false
            )
        ).apply {
            val itemBinding = this.binding as LayoutTdheaderBinding
            itemBinding.event = this@TabTeamHeaderAdapter

        }
    }

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {

        tdBean1?.let {
            val binding = holder.binding as LayoutTdheaderBinding
            binding.event = this
            this.tdBean1!!.apply {
                binding.teamNum = team_num
                binding.userBuy = user_buy
                binding.teamBuy = team_buy
                binding.directBuy = direct_buy
            }

            StoreUtil.getBean<UserInfo>(context)?.let {
                binding.username = it.nickname
                binding.phone = it.phone
                binding.levelStr = it.level_str
                binding.fDate = it.f_date

                if (it.avatar != null) {
                    manager.load(it.avatar).error(R.mipmap.ic_launcher)
                        .into(binding.userAvatar)
                } else {
                    binding.userAvatar.setImageResource(R.mipmap.ic_launcher)
                }
            }
        }

    }

    fun tuiguang() {
        context.startActivity(Intent(context, TuiGuangActivity::class.java))
    }

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
        ) {
            lp.isFullSpan = true
        }
    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }

}